﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel.Composition;
using System.Reflection;

using org.theGecko.Utilities.MefMvc;

using LeMigue.Scrum.Iterface.Model;
using LeMigue.Scrum.Iterface.Enum;

using LeMigue.TeamPortal.Base.Mvc;
using LeMigue.TeamPortal.Base.Globalization;
using LeMigue.TeamPortal.Base.View;
using LeMigue.TeamPortal.Base.Biz.Interface;
using LeMigue.TeamPortal.Base.Biz.Implement;
using LeMigue.Framework.Utils;
using LeMigue.TeamPortal.Base.Biz;

namespace LeMigue.TeamPortal.ScrumBoard.Controllers
{
    //TODO: para que serve este "HandleError"
    /*[HandleError]*/
    [ControllerMetaData("ScrumBoard")]
    [Export(typeof(IController)), PartCreationPolicy(CreationPolicy.NonShared)]
    [Authorize]
    [CheckTeam]
    public class ScrumBoardController : TeamPortalController
    {

        private IScrumBoardBiz ScrumBoardBiz = FactoryBiz.ScrumBoardBiz;
        private ITaskBiz TaskBiz = FactoryBiz.TaskBiz;
        private IIterationBiz IterationBiz = FactoryBiz.IterationBiz;

        private int IdSprint{
            get { return CurrentContext.Get<int>("idSprint", 0); }
            set { CurrentContext.Set<int>("idSprint", value); }
        }

        private Sprint SprintAtivo
        {
            get{
                return ScrumBoardBiz.SprintAtivo(Team, IdSprint);
            }

        }

        [MainMenu("MenuScrumBoard", 1)]
        public ActionResult Index(int? id)
        {
            if (id.HasValue)
                IdSprint = id.Value;

            return View();
        }


        [ChildActionOnly]
        public PartialViewResult Board()
        {
            return PartialView(new IterationView(SprintAtivo.Iteration));
        }


        [ChildActionOnly]
        public PartialViewResult ListBoard()
        {

            IList<IItemListBoardView> itensBacklog = ScrumBoardBiz.ListItemBoard(SprintAtivo);

            if (itensBacklog.Count <= 0)
               return PartialView("EmptyListBoard");

            return PartialView(itensBacklog);
        }
        [ChildActionOnly]
        public PartialViewResult ItemBoard(IItemListBoardView item)
        {
            return PartialView(item);
        }

        [ChildActionOnly]
        public PartialViewResult OrfanTaskBoard()
        {
            if (!SprintAtivo.HasTask)
                return PartialView("Empty");

            return PartialView();
        }
        [ChildActionOnly]
        public PartialViewResult OrfanListTaskBoard(TaskState? state)
        {
            IList<TaskView> tasks = TaskBiz.List(SprintAtivo, state);
            //

            if (tasks.Count <= 0)
                return PartialView("Empty");

            return PartialView("ListTaskBoard", tasks);
        }

        public PartialViewResult ContentItemBoard(int? id)
        {
            IItemListBoardView item = ScrumBoardBiz.ItemBoard(id);

            return PartialView(item);
        }

        [ChildActionOnly]
        public PartialViewResult SubListBoard(IItemListBoardView item)
        {
            IList<IItemListBoardView> itensBacklog = ScrumBoardBiz.ListItemBoard(item);

            if (itensBacklog.Count <= 0)
                return PartialView("Empty");

            return PartialView(itensBacklog);
        }
        [ChildActionOnly]
        public PartialViewResult ItemTaskBoard(IItemListBoardView item)
        {
            if (((IHasTask)item.Reference).HasTask)
                return PartialView(item);

            return PartialView("Empty");
        }
        [ChildActionOnly]
        public PartialViewResult ListTaskBoard(IItemListBoardView item, TaskState? state)
        {
            IList<TaskView> tasks = ScrumBoardBiz.Tasks(item, state);

            if (tasks.Count <= 0)
                return PartialView("Empty");

            return PartialView(tasks);
        }
        [ChildActionOnly]
        public PartialViewResult EmptyItemBoard(IItemListBoardView item)
        {

            if (!((IHasBacklog)item.Reference).HasBacklog && !((IHasTask)item.Reference).HasTask)
                return PartialView();

            return PartialView("Empty");
        }
        
        
        
    }

}
